<?php

class CartrigeController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	$this->view->title = "Список картриджей";
        $this->view->headTitle($this->view->title, 'PREPEND'); 
    	
        $cartrige = new Model_Cartrige();
        $this->view->cartriges = $cartrige->getAllCartrigesByGroup('name');
    }
    
    public function modellistAction()
    {
    	$modelname = $this->_getParam('modelname');
    	$this->view->title = "Список картриджей модели ".$modelname;
        $this->view->headTitle($this->view->title, 'PREPEND'); 
    	
        $cartrige = new Model_Cartrige();
        $this->view->cartriges = $cartrige->getAllCartrigesByModel($modelname);
    }
    
	public function addAction()
	{
		$this->view->title = "Форма добавления картриджа";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        $form = new Form_Cartrige();
        
        if ($this->getRequest()->isPost())
        {
        	if ($form->isValid($this->getRequest()->getPost()))
        	{
        		$cartrige = new Model_Cartrige();	
        		$cartrige->fill($form->getValues());        		
        		$cartrige->save();
        		$this->_helper->redirector('index');
        	}        	
        }
        
        $this->view->form = $form; 		
	}
	
	public function editAction()
	{
		$this->view->title = "Форма добавления картриджа";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Form_Cartrige();
        $form->getElement('submit')->setLabel('Сохранить');
        
        if ($this->getRequest()->isPost())
        {
        	if ($form->isValid($this->getRequest()->getPost()))
        	{
        		$id = $this->_getParam('id');
        		$cartrige = new Model_Cartrige($id);	
        		$cartrige->fill($form->getValues());        		
        		$cartrige->save();
        		$this->_helper->redirector('index');
        	}        	
        } else {
        	$id = $this->_getParam('id');
        	$cartrige = new Model_Cartrige($id);	
        	$form->populate($cartrige->populateForm());	
        }
        
        $this->view->form = $form;
		
	}
	
	public function deleteAction()
	{
		$id = $this->_getParam('id');
        $cartrige = new Model_Cartrige($id);
        $cartrige->delete();
        $this->_helper->redirector('index');
	}
	
	public function viewAction()
	{
		$id = $this->_getParam('id');
		/*
		$id = $this->_getParam('id');
        $cartrige = new Model_Cartrige($id);
        $statistic = $cartrige->getStatistic();
        $this->view->statistic = $statistic;
        */
		$this->view->id = $id;
	}

}

